/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DriverFrame.java
 *
 * Created on Jul 22, 2012, 7:24:44 PM
 */

package vehicles;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author jonesb
 */
public class DriverFrame extends javax.swing.JFrame {

    // An ArrayList to hold all cars.
    ArrayList<Car> allCars = new ArrayList<Car>();
    DefaultComboBoxModel dcmb = new DefaultComboBoxModel();

    /** Creates new form DriverFrame */
    public DriverFrame() {
        initComponents();
        // link the DCMB with the cmbAllCars, so that cmbAllCars will go to dcmb to find data.
        cmbAllCars.setModel(dcmb);

        // Start the thread to check for warnings.
        Warnings warnings = new Warnings();
        Thread warningThread = new Thread(warnings);
        warningThread.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        btnCreateCar = new javax.swing.JButton();
        txtDistance = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnMoveCars = new javax.swing.JButton();
        cmbAllCars = new javax.swing.JComboBox();
        lblLowGasWarning = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cmbCarTypes = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtOdometer = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMilesPerGallon = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtGallonsOfGas = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnCreateCar.setText("Create Car");
        btnCreateCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateCarActionPerformed(evt);
            }
        });

        jLabel6.setText("Distance to Move:");

        btnMoveCars.setText("Move Cars");
        btnMoveCars.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveCarsActionPerformed(evt);
            }
        });

        cmbAllCars.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(cmbAllCars, 0, 380, Short.MAX_VALUE)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(btnCreateCar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(txtDistance, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnMoveCars))
                    .add(lblLowGasWarning))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnCreateCar)
                    .add(jLabel6)
                    .add(txtDistance, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnMoveCars))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cmbAllCars, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblLowGasWarning)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jLabel1.setText("Car Simulator");
        getContentPane().add(jLabel1, java.awt.BorderLayout.PAGE_START);

        jPanel1.setLayout(new java.awt.GridLayout(0, 2));

        jLabel2.setText("Type of Car:");
        jPanel1.add(jLabel2);

        cmbCarTypes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Neon", "Insight", "Cavalier", "Prius" }));
        jPanel1.add(cmbCarTypes);

        jLabel3.setText("Odometer:");
        jPanel1.add(jLabel3);
        jPanel1.add(txtOdometer);

        jLabel4.setText("Miles Per Gallon:");
        jPanel1.add(jLabel4);
        jPanel1.add(txtMilesPerGallon);

        jLabel5.setText("Gallons of Gas:");
        jPanel1.add(jLabel5);
        jPanel1.add(txtGallonsOfGas);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnMoveCarsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveCarsActionPerformed
        // find the distance to move, and convert it to the proper data type.
        String strDistance = txtDistance.getText();
        int intDistance = Integer.parseInt(strDistance);

        // iterate over the allCars collection, and move each car the specified distance.
        for (Car car : allCars) {
            // move the car the specified distance.
            car.go(intDistance);
        }
    }//GEN-LAST:event_btnMoveCarsActionPerformed

    private void btnCreateCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateCarActionPerformed
        // gather information entered by the user about this car.
        String carType = this.cmbCarTypes.getSelectedItem().toString();
        // invoke the static factory method we made in Driver to create an object of a Car, based on the user's selection.
        Car myCar = Driver.createCar(carType);

        // populate the car.
        String strOdometer = txtOdometer.getText();
        int intOdometer = Integer.parseInt(strOdometer);
        myCar.setOdometer(intOdometer);

        String strMilesPerGallon = txtMilesPerGallon.getText();
        int intMilesPerGallon = Integer.parseInt(strMilesPerGallon);
        myCar.setMilesPerGallon(intMilesPerGallon);

        String strGallonsOfGas = txtGallonsOfGas.getText();
        double dblGallonsOfGas = Double.parseDouble(strGallonsOfGas);
        myCar.setGallonsOfGas(dblGallonsOfGas);

        JOptionPane.showMessageDialog(this, "Car Created: " + myCar);

        allCars.add(myCar);
        dcmb.addElement(myCar);

    }//GEN-LAST:event_btnCreateCarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DriverFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateCar;
    private javax.swing.JButton btnMoveCars;
    private javax.swing.JComboBox cmbAllCars;
    private javax.swing.JComboBox cmbCarTypes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblLowGasWarning;
    private javax.swing.JTextField txtDistance;
    private javax.swing.JTextField txtGallonsOfGas;
    private javax.swing.JTextField txtMilesPerGallon;
    private javax.swing.JTextField txtOdometer;
    // End of variables declaration//GEN-END:variables

    /**
     * Thread to check for low gas.
     */
    class Warnings implements Runnable {

        /**
         * The contents of the run method will be run in a separate thread.
         */
        @Override
        public void run() {
            // an endless loop to ensure that the thread will keep running
            while (true) {
                // now, iterate over the cars.
                for (Car car : allCars) {
                    if (car.getGallonsOfGas() < 1) {
                        lblLowGasWarning.setText("Low gas on car: " + car.toString());
                    }
                }
                try {
                    // wait 1 minute before next run.
                    Thread.sleep(60000);
                } catch (InterruptedException ex) {

                    // DOn't worry.  An exception here will just be caught and will restart the loop.
                    Logger.getLogger(DriverFrame.class.getName()).log(Level.SEVERE, null, ex);

                }
            }
        }
    }
    

}
